package com.rdc.qingkou.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;

import com.rdc.qingkou.R;
import com.rdc.qingkou.adapter.OneLevelCommentRecyclerViewAdapter;
import com.rdc.qingkou.entity.Comment;
import com.rdc.qingkou.entity.RetractOrExpandHotCommentBean;
import com.rdc.qingkou.ui.CommentActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论界面
 */

public abstract class BaseCommentFragment extends Fragment {

    protected final String COMMENT = "评论";
    protected final String COMMENT_LIST = "评论列表";
    private OneLevelCommentRecyclerViewAdapter mOneLevelCommentRecyclerViewAdapter;
    public EditText editText;
    private InputMethodManager manager;
    private CommentActivity mActivity;
    protected SwipeRefreshLayout swipeRefresh;

    protected List<Comment> hotCommentList = new ArrayList<>();
    protected List<Comment> recentCommentList = new ArrayList<>();
    protected RetractOrExpandHotCommentBean retractOrExpandHotComment = new RetractOrExpandHotCommentBean();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (CommentActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_comment, container, false);
        manager = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        swipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.comment_swipeRefreshLayout);
        swipeRefresh.setColorSchemeResources(R.color.colorStart);

        editText = (EditText) view.findViewById(R.id.comment_input_editText);

        ImageButton mSendImageButton = (ImageButton) view.findViewById(R.id.comment_send_imageButton);
        mSendImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(editText.getText())) {
                    sendComment();
                    //清除用户输入并且收起键盘
                    editText.getText().clear();
                    manager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                    editText.clearFocus();
                }
            }
        });

        retractOrExpandHotComment.setText("展开热门评论");
        retractOrExpandHotComment.setImageId(R.drawable.button_more_down);

        RecyclerView commentRecyclerView = (RecyclerView) view.findViewById(R.id.commit_recyclerView);
        final LinearLayoutManager layoutManager =
                new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false);
        commentRecyclerView.setLayoutManager(layoutManager);

//        mOneLevelCommentRecyclerViewAdapter =
//                new OneLevelCommentRecyclerViewAdapter(
//                        mActivity,
//                        hotCommentList,
//                        recentCommentList,
//                        retractOrExpandHotComment);

        commentRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                int totalItem = mOneLevelCommentRecyclerViewAdapter.getItemCount();
                if (lastVisibleItemPosition + 1 == totalItem) {
                    //发起网络请求获取数据
                    loadingComments();
                }
            }
        });
        setListener();
        commentRecyclerView.setAdapter(mOneLevelCommentRecyclerViewAdapter);
    }

    /**
     * 为CommentRecyclerView设置各种监听。
     * CommentRecyclerViewAdapter由父类的getCommentRecyclerView()提供。
     */
    protected abstract void setListener();

    /**
     * 上拉加载更多评论
     */
    protected abstract void loadingComments();

    /**
     * 发送评论
     */
    protected abstract void sendComment();

    protected final OneLevelCommentRecyclerViewAdapter getCommentRecyclerViewAdapter() {
        return mOneLevelCommentRecyclerViewAdapter;
    }





}
